﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using System.Runtime.InteropServices;
using Excel;
using LibKetoan;

namespace Ketoan
{
	/// <summary>
	/// Summary description for rCandoitk.
	/// </summary>
	public class rCandoitk : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.ComboBox baocao;
		private System.Windows.Forms.ComboBox chuong;
		private System.Windows.Forms.ComboBox hoatdong;
		private System.Windows.Forms.ComboBox lk;
		private System.Windows.Forms.Button butIn;
		private System.Windows.Forms.Button butKetthuc;
		private System.Windows.Forms.NumericUpDown mm;
		private System.Windows.Forms.NumericUpDown qq;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.CheckBox chitiet;
		private System.Windows.Forms.NumericUpDown yyyy;
		private DataSet ds=new DataSet();
		private string schuong,slk,sql;
		private bool bClear=true;
		private AccessData a;
		Excel.Application oxl;
		Excel._Workbook owb;
		Excel._Worksheet osheet;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public rCandoitk(AccessData acc,string _chuong,string _lk,string _mmyy)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			a=acc;schuong=_chuong;slk=_lk;
			mm.Value=decimal.Parse(_mmyy.Substring(0,2));
			yyyy.Value=decimal.Parse("20"+_mmyy.Substring(2));
			if (mm.Value<4) qq.Value=1;
			else if (mm.Value<7) qq.Value=2;
			else if (mm.Value<10) qq.Value=3;
			else qq.Value=4;
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(rCandoitk));
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.butIn = new System.Windows.Forms.Button();
			this.butKetthuc = new System.Windows.Forms.Button();
			this.baocao = new System.Windows.Forms.ComboBox();
			this.chuong = new System.Windows.Forms.ComboBox();
			this.hoatdong = new System.Windows.Forms.ComboBox();
			this.lk = new System.Windows.Forms.ComboBox();
			this.yyyy = new System.Windows.Forms.NumericUpDown();
			this.mm = new System.Windows.Forms.NumericUpDown();
			this.qq = new System.Windows.Forms.NumericUpDown();
			this.label8 = new System.Windows.Forms.Label();
			this.label9 = new System.Windows.Forms.Label();
			this.label10 = new System.Windows.Forms.Label();
			this.chitiet = new System.Windows.Forms.CheckBox();
			((System.ComponentModel.ISupportInitialize)(this.yyyy)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.mm)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.qq)).BeginInit();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 107);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 16);
			this.label1.TabIndex = 8;
			this.label1.Text = "Tháng :";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(104, 107);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(40, 16);
			this.label2.TabIndex = 10;
			this.label2.Text = "Quí :";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 8);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(56, 16);
			this.label3.TabIndex = 0;
			this.label3.Text = "Báo cáo :";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 32);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(56, 16);
			this.label4.TabIndex = 2;
			this.label4.Text = "Chương :";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(0, 56);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(72, 16);
			this.label5.TabIndex = 4;
			this.label5.Text = "Hoạt động :";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(0, 80);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(72, 16);
			this.label6.TabIndex = 6;
			this.label6.Text = "Loại khoản :";
			this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(176, 107);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(40, 16);
			this.label7.TabIndex = 12;
			this.label7.Text = "Năm :";
			this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// butIn
			// 
			this.butIn.Image = ((System.Drawing.Bitmap)(resources.GetObject("butIn.Image")));
			this.butIn.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butIn.Location = new System.Drawing.Point(71, 160);
			this.butIn.Name = "butIn";
			this.butIn.Size = new System.Drawing.Size(68, 25);
			this.butIn.TabIndex = 15;
			this.butIn.Text = "       In";
			this.butIn.Click += new System.EventHandler(this.butIn_Click);
			// 
			// butKetthuc
			// 
			this.butKetthuc.Image = ((System.Drawing.Bitmap)(resources.GetObject("butKetthuc.Image")));
			this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butKetthuc.Location = new System.Drawing.Point(142, 160);
			this.butKetthuc.Name = "butKetthuc";
			this.butKetthuc.Size = new System.Drawing.Size(68, 25);
			this.butKetthuc.TabIndex = 16;
			this.butKetthuc.Text = "Kết thúc";
			this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
			// 
			// baocao
			// 
			this.baocao.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.baocao.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.baocao.Items.AddRange(new object[] {
														"Tháng",
														"Quí",
														"Năm"});
			this.baocao.Location = new System.Drawing.Point(72, 8);
			this.baocao.Name = "baocao";
			this.baocao.Size = new System.Drawing.Size(192, 21);
			this.baocao.TabIndex = 1;
			this.baocao.KeyDown += new System.Windows.Forms.KeyEventHandler(this.baocao_KeyDown);
			this.baocao.SelectedIndexChanged += new System.EventHandler(this.baocao_SelectedIndexChanged);
			// 
			// chuong
			// 
			this.chuong.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.chuong.Location = new System.Drawing.Point(72, 32);
			this.chuong.Name = "chuong";
			this.chuong.Size = new System.Drawing.Size(192, 21);
			this.chuong.TabIndex = 3;
			this.chuong.KeyDown += new System.Windows.Forms.KeyEventHandler(this.baocao_KeyDown);
			// 
			// hoatdong
			// 
			this.hoatdong.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.hoatdong.Location = new System.Drawing.Point(72, 56);
			this.hoatdong.Name = "hoatdong";
			this.hoatdong.Size = new System.Drawing.Size(192, 21);
			this.hoatdong.TabIndex = 5;
			this.hoatdong.KeyDown += new System.Windows.Forms.KeyEventHandler(this.baocao_KeyDown);
			// 
			// lk
			// 
			this.lk.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lk.Location = new System.Drawing.Point(72, 80);
			this.lk.Name = "lk";
			this.lk.Size = new System.Drawing.Size(192, 21);
			this.lk.TabIndex = 7;
			this.lk.KeyDown += new System.Windows.Forms.KeyEventHandler(this.baocao_KeyDown);
			// 
			// yyyy
			// 
			this.yyyy.BackColor = System.Drawing.SystemColors.HighlightText;
			this.yyyy.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.yyyy.Location = new System.Drawing.Point(216, 104);
			this.yyyy.Maximum = new System.Decimal(new int[] {
																 3000,
																 0,
																 0,
																 0});
			this.yyyy.Minimum = new System.Decimal(new int[] {
																 2000,
																 0,
																 0,
																 0});
			this.yyyy.Name = "yyyy";
			this.yyyy.Size = new System.Drawing.Size(48, 21);
			this.yyyy.TabIndex = 13;
			this.yyyy.Value = new System.Decimal(new int[] {
															   2000,
															   0,
															   0,
															   0});
			this.yyyy.KeyDown += new System.Windows.Forms.KeyEventHandler(this.baocao_KeyDown);
			// 
			// mm
			// 
			this.mm.BackColor = System.Drawing.SystemColors.HighlightText;
			this.mm.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.mm.Location = new System.Drawing.Point(72, 104);
			this.mm.Maximum = new System.Decimal(new int[] {
															   12,
															   0,
															   0,
															   0});
			this.mm.Minimum = new System.Decimal(new int[] {
															   1,
															   0,
															   0,
															   0});
			this.mm.Name = "mm";
			this.mm.Size = new System.Drawing.Size(40, 21);
			this.mm.TabIndex = 9;
			this.mm.Value = new System.Decimal(new int[] {
															 1,
															 0,
															 0,
															 0});
			this.mm.KeyDown += new System.Windows.Forms.KeyEventHandler(this.baocao_KeyDown);
			// 
			// qq
			// 
			this.qq.BackColor = System.Drawing.SystemColors.HighlightText;
			this.qq.Enabled = false;
			this.qq.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.qq.Location = new System.Drawing.Point(144, 104);
			this.qq.Maximum = new System.Decimal(new int[] {
															   4,
															   0,
															   0,
															   0});
			this.qq.Minimum = new System.Decimal(new int[] {
															   1,
															   0,
															   0,
															   0});
			this.qq.Name = "qq";
			this.qq.Size = new System.Drawing.Size(40, 21);
			this.qq.TabIndex = 11;
			this.qq.Value = new System.Decimal(new int[] {
															 1,
															 0,
															 0,
															 0});
			this.qq.KeyDown += new System.Windows.Forms.KeyEventHandler(this.baocao_KeyDown);
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(176, 107);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(40, 16);
			this.label8.TabIndex = 6;
			this.label8.Text = "Năm :";
			this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label9
			// 
			this.label9.Location = new System.Drawing.Point(104, 107);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(40, 16);
			this.label9.TabIndex = 1;
			this.label9.Text = "Quí :";
			this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(16, 107);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(56, 16);
			this.label10.TabIndex = 0;
			this.label10.Text = "Tháng :";
			this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// chitiet
			// 
			this.chitiet.Checked = true;
			this.chitiet.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chitiet.Location = new System.Drawing.Point(72, 134);
			this.chitiet.Name = "chitiet";
			this.chitiet.Size = new System.Drawing.Size(104, 16);
			this.chitiet.TabIndex = 17;
			this.chitiet.Text = "Chi tiết";
			this.chitiet.KeyDown += new System.Windows.Forms.KeyEventHandler(this.baocao_KeyDown);
			// 
			// rCandoitk
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(280, 205);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.chitiet,
																		  this.qq,
																		  this.mm,
																		  this.yyyy,
																		  this.lk,
																		  this.hoatdong,
																		  this.chuong,
																		  this.baocao,
																		  this.butKetthuc,
																		  this.butIn,
																		  this.label7,
																		  this.label6,
																		  this.label5,
																		  this.label4,
																		  this.label3,
																		  this.label2,
																		  this.label1,
																		  this.label8,
																		  this.label9,
																		  this.label10});
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "rCandoitk";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Bảng cân đối tài khoản";
			this.Load += new System.EventHandler(this.rCandoitk_Load);
			this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.rCandoitk_MouseMove);
			((System.ComponentModel.ISupportInitialize)(this.yyyy)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.mm)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.qq)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void baocao_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}

		private void butIn_Click(object sender, System.EventArgs e)
		{	
			if (!a.bYyyy(yyyy.Value.ToString()))
			{
				MessageBox.Show("Số liệu năm "+yyyy.Value.ToString()+" chưa tạo !",a.Msg);
				return;
			}
			string tu=mm.Value.ToString().PadLeft(2,'0'),den=tu;
			switch (baocao.SelectedIndex)
			{
				case 0:den=tu;break;
				case 1:
					switch (int.Parse(qq.Value.ToString()))
					{
						case 1: tu="01";den="03";break;
						case 2: tu="04";den="06";break;
						case 3: tu="07";den="09";break;
						default : tu="10";den="12";break;
					}
					break;
				default : tu="01";den="12";break;
			}
			tu+=yyyy.Value.ToString().PadLeft(4,'0').Substring(2);
			den+=yyyy.Value.ToString().PadLeft(4,'0').Substring(2);
			ds=a.rCandoitk((chuong.SelectedIndex!=-1)?int.Parse(chuong.SelectedValue.ToString()):-1,(hoatdong.SelectedIndex!=-1)?int.Parse(hoatdong.SelectedValue.ToString()):-1,(lk.SelectedIndex!=-1)?int.Parse(lk.SelectedValue.ToString()):-1,tu,den,chitiet.Checked);
			//
			a.check_process_Excel();
			string tenfile=a.Export_Excel(ds,"rCandoitk");
			oxl=new Excel.Application();
			owb=(Excel._Workbook)(oxl.Workbooks.Open(tenfile,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value));
			osheet=(Excel._Worksheet)owb.ActiveSheet;
			oxl.ActiveWindow.DisplayGridlines=true;
			oxl.ActiveWindow.DisplayZeros=false;
			osheet.PageSetup.Orientation=XlPageOrientation.xlLandscape;
			osheet.PageSetup.PaperSize=XlPaperSize.xlPaperA4;
			osheet.PageSetup.LeftMargin=20;
			osheet.PageSetup.RightMargin=20;
			osheet.PageSetup.TopMargin=30;
			osheet.PageSetup.CenterFooter="Trang : &P/&N";
			oxl.Visible=true;
		}

		private void rCandoitk_Load(object sender, System.EventArgs e)
		{
			baocao.SelectedIndex=0;

			sql="select * from a_dmchuong where sudung=1 ";
			if (schuong!="") sql+=" and id in ("+schuong.Substring(0,schuong.Length-1)+")";
			sql+=" order by stt";
			chuong.DisplayMember="TEN";
			chuong.ValueMember="ID";
			chuong.DataSource=a.get_data(sql).Tables[0];

			sql="select * from a_hoatdong where sudung=1 ";
			sql+=" order by stt";
			hoatdong.DisplayMember="TEN";
			hoatdong.ValueMember="ID";
			hoatdong.DataSource=a.get_data(sql).Tables[0];

			sql="select * from a_dmlk where sudung=1 ";
			if (slk!="") sql+=" and id in ("+slk.Substring(0,slk.Length-1)+")";
			sql+=" order by stt";
			lk.DisplayMember="LK";
			lk.ValueMember="ID";
			lk.DataSource=a.get_data(sql).Tables[0];

		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void rCandoitk_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (bClear) 
			{
				chuong.SelectedIndex=hoatdong.SelectedIndex=lk.SelectedIndex=-1;		
				bClear=false;
			}
		}

		private void baocao_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==baocao)
			{
				if (baocao.SelectedIndex==2) mm.Enabled=qq.Enabled=false;
				else
				{
					mm.Enabled=baocao.SelectedIndex==0;
					qq.Enabled=!mm.Enabled;				
				}
			}
		}
	}
}
